<template>
  <div class="register">
    <!-- 背景图 -->
    <div class="icon">
      <backTitle></backTitle>
      <span class="fuhao">
        <i class="iconfont icon-yinlebofangxuanlv"></i>
      </span>
    </div>

    <!-- 注册框 -->
    <main>
      <div class="registerkuang">
        <van-cell-group class="group">
          <van-field
            v-model="username"
            left-icon="user-o"
            placeholder="请输入用户名"
            right-icon="warning-o"
            label-width="60px"
            clearable
          />
          <van-field
            v-model="password"
            type="password"
            left-icon="bag-o"
            placeholder="请输入密码"
            right-icon="warning-o"
            label-width="60px"
            clearable
          />
          <van-field
            v-model="phone"
            type="tel"
            left-icon="phone-o"
            placeholder="请输入手机号"
            right-icon="warning-o"
            label-width="60px"
            clearable
          />
          <van-field
            v-model="sms"
            type="digit"
            center
            clearable
            left-icon="chat-o"
            placeholder="请输入短信码"
            label-width="80px"
          >
            <van-button
              slot="button"
              size="small"
              type="primary"
              v-show="countshow"
              @click="msg"
            >发送验证码</van-button>
            <van-button slot="button" size="small" type="primary" v-show="!countshow">{{count}}s</van-button>
          </van-field>
        </van-cell-group>
        <div class="btn">
          <button class="btnregister" @click="clickregister">注册</button>
        </div>
      </div>
    </main>
  </div>
</template>

<script>
import backTitle from "../../components/back";
import send from "../../navshow";

export default {
  data() {
    return {
      username: "", //用户名
      password: "", //密码
      phone: "", //手机号
      sms: "", //短信验证码
      countshow: true,
      count: "",
      timer: null
    };
  },
  created() {
    // 隐藏导航栏
    send.$emit("tab-show", false);
    // 隐藏播放器
    this.$store.commit("showplayer", false);
  },
  methods: {
    // 注册
    clickregister() {
      if (this.username == "") {
        this.$toast("请输入用户名");
      } else if (this.password == "") {
        this.$toast("请输入密码");
      } else if (this.phone == "") {
        this.$toast("请输入手机号");
      } else if (this.sms == "") {
        this.$toast("请输入短信验证码");
      } else {
        // 验证验证码
        var checksms =
          "http://localhost:3000/captcha/verify?phone=" +
          this.phone +
          "&captcha=" +
          this.sms;
        this.axios
          .get(checksms)
          .then(res => {
            if (res.status == 200) {
              // 注册账号
              let url =
                "http://localhost:3000/register/cellphone?phone=" +
                this.phone +
                "&password=" +
                this.password +
                "&captcha=" +
                this.sms +
                "&nickname=" +
                this.username;
              this.axios({
                url,
                withCredentials: true //关键
              })
                .then(res => {
                  if (res.status == 200) {
                    $this.$toast("注册成功");
                    this.$router.push("/login/login");
                  } else {
                    this.$toast("注册失败");
                  }
                })
                .catch(err => {
                  console.log(err);
                });
            }
          })
          .catch(err => {
            this.$toast("验证码输入错误");
          });
      }
    },

    // 发送验证码
    msg() {
      // 验证手机是否已经注册
      var url =
        "http://localhost:3000/cellphone/existence/check?phone=" + this.phone;

      if (this.phone) {
        this.axios.get(url).then(res => {
          var regexp = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/;
          if (regexp.test(this.phone)) {
            console.log(res.data);
            if (res.data.exist == 1) {
              this.$toast("该手机号已经注册");
            } else {
              const TIME_COUNT = 60;
              if (!this.timer) {
                this.count = TIME_COUNT;
                this.countshow = false;
                this.timer = setInterval(() => {
                  if (this.count > 0 && this.count <= TIME_COUNT) {
                    this.count--;
                  } else {
                    this.countshow = true;
                    clearInterval(this.timer);
                    this.timer = null;
                  }
                }, 1000);
              }
              // 发送验证码
              var smsurl =
                "http://localhost:3000/captcha/sent?phone=" + this.phone;
              this.axios
                .get(smsurl)
                .then(res => {
                  this.$toast("验证码发送成功");
                })
                .catch(err => {
                  this.$toast("验证码发送失败");
                });
            }
          } else {
            this.$toast("手机号输入错误请重新输入");
          }
        });
      } else {
        this.$toast('手机号不能为空')
      }
    }
  },
  components: {
    backTitle
  }
};
</script>

<style>
.register {
  width: 100vw;
  height: 100vh;
  background-color: #eeeeee;
}

/* 头部 */
.register .back {
  display: block;
  width: 50px;
  height: 50px;
  position: absolute;
  top: 10px;
  left: -25px;
  text-align: center;
  color: #231f20;
  font-weight: bold;
}

/* 背景图 */
.register .icon {
  width: 100%;
  height: 45vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-image: url("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1843625595,2022074812&fm=26&gp=0.jpg");
  background-size: 100% 110%;
}

.register .fuhao {
  display: block;
  width: 100px;
  height: 100px;
  text-align: center;
  border-radius: 50px;
  background: #c0c0c0;
  box-shadow: 20px 20px 60px #686868, -20px -20px 60px #ffffff;
}

.register .fuhao i {
  font-size: 70px;
}

/* 注册框 */
.register .group {
  width: 85vw;
  margin: 15px auto 15px;
}

.group > div {
  padding-top: 15px;
  border-bottom: 1px solid lightgray;
}

.register .btn {
  width: 100vw;
  margin: 0 auto 0;
}

.register .btnregister {
  width: 50vw;
  height: 40px;
  color: white;
  font-size: 18px;
  border: 0;
  border-radius: 50px;
  background: #231f20;
}

.register .van-cell {
  background-color: #eeeeee;
}

.register input {
  font-size: 14px;
}
</style>